package com.ws.shop.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.StatementCallback;
import org.springframework.stereotype.Repository;

import com.ws.shop.dao.AdminMenuDao;
import com.ws.shop.entity.AdminMenu;
@Repository(value="adminMenuDao")
public class AdminMenuDaoImpl extends BaseDaoImpl implements AdminMenuDao{

	public Object[] delMenu(final int menuid) {
		return this.execute(new StatementCallback<Object[]>(){
			public Object[] doInStatement(Statement stmt) throws SQLException,
					DataAccessException {
				ResultSet rs = stmt.executeQuery("select count(1) from admin_menu where pmenuid="+menuid) ;
				int count = 0;
				if(rs.next()){
					count = rs.getInt(1);
				}
				if(count > 0){
					return new Object[]{1,"该菜单包含有子菜单，请先删除子菜单"};
				}
				stmt.executeUpdate("delete from admin_menu where menuid="+menuid);
				return new Object[]{0,"删除成功"};
			}
		});
	}

	final String getAllAdminMenus = "select menuid,menuname,descs,menutype,status,pmenuid,linkaddr,ord,creattime from admin_menu order by ord";
	public List<AdminMenu> getAllAdminMenus() {
		return this.query(getAllAdminMenus, new ResultSetExtractor<List<AdminMenu>>(){
			public List<AdminMenu> extractData(ResultSet rs)
					throws SQLException, DataAccessException {
				List<AdminMenu> list = new LinkedList<AdminMenu>();
				AdminMenu am = null;
				while(rs.next()){
					am = new AdminMenu();
					am.setMenuid(rs.getInt(1));
					am.setMenuname(rs.getString(2));
					am.setDescs(rs.getString(3));
					am.setMenutype(rs.getInt(4));
					am.setStatus(rs.getInt(5));
					am.setPmenuid(rs.getInt(6));
					am.setLinkaddr(rs.getString(7));
					am.setOrd(rs.getInt(8));
					am.setCreattime(rs.getTimestamp(9));
					list.add(am);
				}
				return list;
			}
		});
	}

	public AdminMenu getMenuById(final int menuid) {
		return this.query("select menuname,descs,menutype,status,pmenuid,linkaddr,ord,creattime from admin_menu where menuid=?",
				new ResultSetExtractor<AdminMenu>(){
					public AdminMenu extractData(ResultSet rs) throws SQLException,
							DataAccessException {
						if(!rs.next())
							return null;
						AdminMenu am = new AdminMenu();
						am.setMenuid(menuid);
						am.setMenuname(rs.getString(1));
						am.setDescs(rs.getString(2));
						am.setMenutype(rs.getInt(3));
						am.setStatus(rs.getInt(4));
						am.setPmenuid(rs.getInt(5));
						am.setLinkaddr(rs.getString(6));
						am.setOrd(rs.getInt(7));
						am.setCreattime(rs.getTimestamp(8));
						return am;
					}
				}, menuid);
	}

	public void saveOrUpdate(AdminMenu menu) {
		if(menu.getMenuid() == 0){
			this.update("insert into admin_menu(menuname,descs,menutype,status,pmenuid,linkaddr,ord,creattime) values (?,?,?,?,?,?,?,now())",
					menu.getMenuname(),menu.getDescs(),menu.getMenutype(),menu.getStatus(),menu.getPmenuid(),menu.getLinkaddr(),menu.getOrd());
		}else{
			this.update("update admin_menu set menuname=?,descs=?,menutype=?,status=?,pmenuid=?,linkaddr=?,ord=? where menuid=?",
					menu.getMenuname(),menu.getDescs(),menu.getMenutype(),menu.getStatus(),menu.getPmenuid(),menu.getLinkaddr(),menu.getOrd(),menu.getMenuid());
		}
	}

}
